﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.WebModel;
using System.Web.Services;
using System.Web.Script.Services;
using TrainingsApp.Helper;
using TrainingsApp.Model;
using TrainingsApp.Logic;

namespace TrainingsApp.IPADApp.Student {
    public partial class Preview_Ask : AccountPage {
        protected string ChapterId { get; set; }

        protected void Page_Load(object sender, EventArgs e) {
            Guid pId = Guid.Empty;
            ChapterId = string.Empty;
            if (Guid.TryParse(ZJFramework10.Utility.QueryStringManager.GetValue<string>("p"), out pId)) {
                var previews = TrainingsApp.Logic.TS_Preview_Logic.TS_Preview_View(pId);
                if (previews != null) {
                    ChapterId = previews.ChapterId.Value.ToString();
                }
            }

        }

        public override WebModel.Other.AccountMenuType MenuType {
            get { return WebModel.Other.AccountMenuType.Preview; }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
        public static string AnswerAdd(string PreviewId, string UserId, string Result) {
            bool boolIsSucess = true;
            string strBackMessage = string.Empty;
            if (PassPort.IsAuthenticated) {
                try {
                    TS_Question_Entity q = TS_Question_Logic.TS_Question_ViewByPreviewId(new Guid(PreviewId));
                    TS_Preview_Result_Logic.TS_Preview_Result_Add(new TS_Preview_Result_Entity() {
                        PreviewId = new Guid(PreviewId),
                        UserId = PassPort.CurrentUser.UserID,
                        QuestionId = q.QuestionId,
                        Result = Result,
                        AnswerDate = DateTime.Now,
                        GroupId = 99
                    });
                }
                catch (Exception ex) {
                    boolIsSucess = false;
                    strBackMessage = ex.Message.Replace("\r", "").Replace("\n", "");
                }
            }
            else {
                boolIsSucess = false;
                strBackMessage = "您已经登录超时，请重新登录系统。";
            }
            return "{" + string.Format("IsSuccess:{0},Msg:'{1}'", boolIsSucess.ToString().ToLower(), strBackMessage) + "}";
        }
    }
}